﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SHVS.DisplayData;
using VLIB28D.Geometry3D;
using VLIB28D.Geometry3D;
using VLIB2D.Geometry2D;

namespace GMPP.utils
{
    public class SkyDome
    {
        public float Radians = (float)Math.PI / 180;

        public int NumPoints { get; set; }

        public Point3D[] Vertices { get; set; }

        public Point2D[] TexCoords { get; set; }

        public Vector3D[] NormalVectors { get; set; }

        public string DomeTex { get; set; }

        public Triangles5D Triangle5Ds { get; set; }

        public SkyDome(float thetaAmt, float phiAmt, float radius,string tex)
        {
            DomeTex = tex;
            Initialize(thetaAmt, phiAmt, radius);
            BuildMesh();
        }

        private bool Initialize(float thetaAmt, float phiAmt, float radius)
        {

            int index = 0;

            NumPoints = (int)((360 / phiAmt) * (90 / thetaAmt) * 4);//三角形带,所以乘以4

            Vertices = new Point3D[NumPoints];
            TexCoords = new Point2D[NumPoints];
            NormalVectors=new Vector3D[NumPoints/4];
            for (int theta = 0; theta <= (90 - thetaAmt); theta += (int)thetaAmt)
            {
                for (int phi = 0; phi <= (360 - phiAmt); phi += (int)phiAmt)
                {
                    NormalVectors[index/4]=new Vector3D(1,1,1);
                    var ve = new Vector3D(phi * Radians, theta * Radians, radius,"");
                    Vertices[index] = new Point3D(ve);
                    NormalVectors[index / 4] = ve;
                    /**计算纹理坐标**/
                    TexCoords[index] = new Point2D((1+ve.UnitVector.X)/2, (1+ve.UnitVector.Z)/2);
                    //ComputeTextureCoord((index * 2), (index * 2) + 1, ve);
                    index++;



                    ve = new Vector3D(phi * Radians, (theta + thetaAmt) * Radians, radius, "");
                    Vertices[index] = new Point3D(ve);

                    /**计算纹理坐标**/
                    TexCoords[index] = new Point2D((1 + ve.UnitVector.X) / 2, (1 + ve.UnitVector.Z) / 2);
                    //ComputeTextureCoord((index * 2), (index * 2) + 1, ve);
                    index++;




                    ve = new Vector3D((phi + phiAmt) * Radians, theta * Radians, radius, "");
                    Vertices[index] = new Point3D(ve);

                    /**计算纹理坐标**/
                    TexCoords[index] = new Point2D((1 + ve.UnitVector.X) / 2, (1 + ve.UnitVector.Z) / 2);
                    //ComputeTextureCoord((index * 2), (index * 2) + 1, ve);
                    index++;

                    if (theta < 90)
                    {
                        ve = new Vector3D((phi + phiAmt) * Radians, (theta + thetaAmt) * Radians, radius, "");
                        Vertices[index] = new Point3D(ve);

                        /**计算纹理坐标**/
                        TexCoords[index] = new Point2D((1 + ve.UnitVector.X) / 2, (1 + ve.UnitVector.Z) / 2);
                        //ComputeTextureCoord((index * 2), (index * 2) + 1, ve);
                        index++;
                    }
                }
            }
            return true;
        }

        private void BuildMesh()
        {
            Triangle5Ds = new Triangles5D();
            var num = Vertices.Length/4;
            for (int i = 0; i <num; i++)
            {
                var leftTriangle = new Triangle5D()
                    {
                        V1 = new Vertex5D(Vertices[4 * i + 1], TexCoords[4 * i + 1],NormalVectors[i]),
                        V2 = new Vertex5D(Vertices[4 * i + 0], TexCoords[4 * i + 0], NormalVectors[i]),
                        V3 = new Vertex5D(Vertices[4 * i + 2], TexCoords[4 * i + 2], NormalVectors[i]),
                        
                        Material = DomeTex
                    };
                var rightTriangle = new Triangle5D()
                {
                    V1 = new Vertex5D(Vertices[4 * i + 1], TexCoords[4 * i + 1], NormalVectors[i]),
                    V2 = new Vertex5D(Vertices[4 * i + 2], TexCoords[4 * i + 2], NormalVectors[i]),
                    V3 = new Vertex5D(Vertices[4 * i + 3], TexCoords[4 * i + 3], NormalVectors[i]),
                    Material = DomeTex
                };
                Triangle5Ds.Add(leftTriangle);
                Triangle5Ds.Add(rightTriangle);
            }


        }

        //public void Normalize(float[] vec)
        //{
        //    var ve = new Vector3D(vec[0], vec[1], vec[2]);
        //    var unitVe = ve.UnitVector;
        //    vec[0] = (float)unitVe.X;
        //    vec[1] = (float)unitVe.Y;
        //    vec[2] = (float)unitVe.Z;
        //}

        //public void ComputeTextureCoord(int tu, int tv, Vector3D ve)
        //{
        //    v[nIndex].mTX = tu * fAlpha / (2.0f * D3DX_PI);
        //    v[nIndex].mTY = tv * fBeta / (D3DX_PI);
        //}


    }
}
